import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';
import 'package:flutter_09_game/widgets/btn_dropdown.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class WidgetBetRecordDropdown extends StatelessWidget {
  const WidgetBetRecordDropdown({
    super.key,
    required this.teamRows,
    required this.teamRx,
    required this.statusRows,
    required this.timeRows,
    required this.symbolRows,
    required this.symbolRx,
    required this.timeRx,
    required this.statusRx,
    required this.gameRows,
    required this.gameRx,
    required this.editingController,
    required this.onRequestRefresh,
    required this.onTimeSelection,
    this.userEditingController,
  });

  final List<String> teamRows;
  final List<String> symbolRows;
  final List<String> statusRows;
  final List<String> timeRows;
  final List<String> gameRows;

  final RxString symbolRx;
  final RxString statusRx;
  final RxString timeRx;
  final RxString gameRx;
  final RxString teamRx;
  final Function(String) onTimeSelection;
  final Function(String) onRequestRefresh;

  final TextEditingController editingController;
  final TextEditingController? userEditingController;
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(16.w).copyWith(bottom: 24.w),
      decoration: BoxDecoration(
        color: AppColor.navBgColor,
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(30.w),
          bottomRight: Radius.circular(30.w),
        ),
      ),
      child: Obx(() {
        return Column(
          children: [
            _buildTopDropdown(teamRows,
                value: teamRx.value,
                width: 1.sw - 32.w,
                btnColor: AppColor.primary, onChanged: (value) {
              teamRx.value = value;
            }),
            4.verticalSpaceFromWidth,
            Row(
              children: [
                Expanded(
                    child: _buildTopDropdown(symbolRows,
                        value: symbolRx.value.isEmpty
                            ? LocaleKeys.allCoins.tr
                            : symbolRx.value,
                        // mainAxisAlignment: MainAxisAlignment.start,
                        onChanged: (value) {
                  // clog('dalsdjl  $value');
                  if (value == LocaleKeys.allCoins.tr) {
                    symbolRx.value = '';
                    // clog('dalsdjl  $value');
                  } else {
                    symbolRx.value = value;
                  }
                })),
                4.horizontalSpace,
                Expanded(
                    child: _buildTopDropdown(statusRows, value: statusRx.value,
                        onChanged: (value) {
                  statusRx.value = value;
                })),
                4.horizontalSpace,
                Expanded(
                    child: _buildTopDropdown(timeRows,
                        btnColor: AppColor.primary,
                        value: timeRx.value, onChanged: (value) {
                  // logic.updateTimeSelection(value);
                  onTimeSelection.call(value);
                  // updateTimeSelection
                })),
              ],
            ),
            4.verticalSpaceFromWidth,
            Row(
              children: [
                Expanded(
                    child: _buildTopDropdown(gameRows, value: gameRx.value,
                        onChanged: (value) {
                  gameRx.value = value;
                })),
                if (userEditingController != null)
                  Expanded(
                    child: _buildInput(
                        userEditingController!, LocaleKeys.memberId.tr)
                        .marginOnly(left: 4.w),
                  ),
                4.horizontalSpace,
                Expanded(
                  child: _buildInput(
                      editingController, LocaleKeys.betOrderNumber.tr),
                )
              ],
            )
          ],
        );
      }),
    );
  }

  Widget _buildInput(TextEditingController controller, String hintText) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 6.w, vertical: 6.5.w)
          .copyWith(left: 12.w),
      decoration: BoxDecoration(
          border: Border(
              bottom: BorderSide(
                  color: AppColor.textPrimary.withOpacity(0.15), width: 1)),
          color: Colors.black.withOpacity(0.3),
          borderRadius: BorderRadius.circular(8.w)),
      child: Row(
        children: [
          Expanded(
              child: TextField(
                controller: controller,
                maxLength: 30,
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w500,
                  color: AppColor.textPrimary.withOpacity(0.94),
                ),
                decoration: InputDecoration(
                  border: InputBorder.none,
                  enabledBorder: InputBorder.none,
                  focusedBorder: InputBorder.none,
                  disabledBorder: InputBorder.none,
                  errorBorder: InputBorder.none,
                  focusedErrorBorder: InputBorder.none,
                  counterText: '',
                  hintText: hintText,
                  isDense: true,
                  // 紧凑布局，有助于垂直对齐
                  hintStyle: TextStyle(
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w400,
                    color: AppColor.textPrimary.withOpacity(0.5),
                  ),
                  contentPadding: EdgeInsets.zero, // 调整内边距
                ),
                onSubmitted: (text) {
                  onRequestRefresh.call(text);
                  // logic.refreshController.requestRefresh();
                },
              )),
          GestureDetector(
            onTap: () {
              onRequestRefresh.call(controller.text);
              // logic.refreshController.requestRefresh();
            },
            child: AutoImage(
              Assets.commonSearchBtn,
              width: 32.w,
            ),
          )
        ],
      ),
    );
  }


  Widget _buildTopDropdown(List<String> items,
      {required String value,
      required Function(String) onChanged,
      MainAxisAlignment? mainAxisAlignment,
      double? width,
      Color? btnColor}) {
    return BtnDropdown(
        items: items,
        value: value,
        width: width,
        // width: 1.sw - 32.w,
        mainAxisAlignment: mainAxisAlignment,
        onChanged: (value) {
          onChanged.call(value);
        },
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 12.w),
          decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(
                      color: AppColor.textPrimary.withOpacity(0.15), width: 1)),
              color: Colors.black.withOpacity(0.3),
              borderRadius: BorderRadius.circular(8.w)),
          child: Row(
            children: [
              Expanded(
                  child: Text(
                value,
                style: TextStyle(
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w900,
                  color: btnColor ?? AppColor.textPrimary.withOpacity(0.94),
                ),
              )),
              AutoImage(
                Assets.promotionCommissionCaretDown,
                width: 16.w,
              )
            ],
          ),
        ));
  }
}
